//
//  RootViewController.m
//  智慧银行
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 weilai. All rights reserved.
//

#import "RootViewController.h"
#import "ButtonTableViewCell.h"
#import "HomeViewController.h"
#import "NewsViewController.h"
#import "ServeViewController.h"
#import "CardViewController.h"
#import "PayViewController.h"
#import "GoodsViewController.h"
#import "MyViewController.h"
#import "TalkViewController.h"
#import "TipsViewController.h"
#import "QuestionViewController.h"
#import "SetViewController.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
#define MAIN_COLOR [UIColor colorWithRed:86/255.0 green:54/255.0 blue:6/255.0 alpha:1]
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView            *mainTableView;
@property (nonatomic, strong)NSArray                *lDataSource;
@property (nonatomic, strong)NSArray                *fDataSource;
@property (nonatomic, strong)UIButton               *loginButton;
@property (nonatomic, strong)UIButton               *menuButton;
@property (nonatomic, strong)UIButton               *setButton;
@property (nonatomic, strong)UILabel                *label;
@property (nonatomic, strong)NSMutableArray         *vcArray;
@property (nonatomic, strong)HomeViewController     *homeVC;
@property (nonatomic, strong)NewsViewController     *newsVC;
@property (nonatomic, strong)ServeViewController    *serveVC;
@property (nonatomic, strong)CardViewController     *cardVC;
@property (nonatomic, strong)PayViewController      *payVC;
@property (nonatomic, strong)GoodsViewController    *goodsVC;
@property (nonatomic, strong)MyViewController       *myVC;
@property (nonatomic, strong)TalkViewController     *talkVC;
@property (nonatomic, strong)TipsViewController     *tipVC;
@property (nonatomic, strong)QuestionViewController *questionVC;
@property (nonatomic, strong)SetViewController      *setVC;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlaizeDataSource];
    [self initlaizeuserinterface];
}

- (void)initlaizeDataSource
{
    _lDataSource = [NSArray array];
    _lDataSource = @[@"首页-1",@"金融资讯-1",@"理财服务-1",@"银行卡-1",@"代理产品-1",@"我的网银-1"];
    _fDataSource = @[@"首页",@"金融资讯",@"理财服务",@"银行卡",@"代理产品",@"我的网银"];
    _vcArray = [NSMutableArray array];
    
}

- (void)initlaizeuserinterface
{
    self.view.backgroundColor = MAIN_COLOR;
    
    UIImageView *head = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    head.image = [UIImage imageNamed:@"头像"];
    [self.view addSubview:head];
    
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 75, WIDTH, HEIGHT-75) style:UITableViewStylePlain];
    _mainTableView.backgroundColor = MAIN_COLOR;
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTableView.bounces = NO;
    _mainTableView.rowHeight = 70;
    _mainTableView.showsHorizontalScrollIndicator = NO;
    _mainTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_mainTableView];
    
    _setButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _setButton.frame = CGRectMake(0, 800, 65, 70);
    [_setButton setBackgroundImage:[UIImage imageNamed:@"设置-1"] forState:UIControlStateNormal];
    [_setButton addTarget:self action:@selector(handleSetButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.frame = CGRectMake(0, 870, 65, 70);
    [_loginButton setBackgroundImage:[UIImage imageNamed:@"登录-1"] forState:UIControlStateNormal];
    [self.view addSubview:_loginButton];
    [_loginButton addTarget:self action:@selector(handleLoginButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    _menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _menuButton.frame = CGRectMake(0, 940, 65, 70);
    [_menuButton setBackgroundImage:[UIImage imageNamed:@"滑动-1"] forState:UIControlStateNormal];
    _menuButton.tag = 201;
    [self.view addSubview:_menuButton];
    [_menuButton addTarget:self action:@selector(handleMenuButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    NSArray *arr = @[@"登录",@"ver:1.1.0"];
    for (int i = 0; i < 1; i++) {
        for (int j = 0; j < 2; j ++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80, 870 + (j*70), 80, 40)];
            label.text = arr[j];
            label.textColor = [UIColor whiteColor];
            [self.view addSubview:label];
        }
    }
    _label = [[UILabel alloc]initWithFrame:CGRectMake(80, 810, 80, 40)];
    _label.text = @"设置";
    _label.textColor = [UIColor whiteColor];
    
    _homeVC = [[HomeViewController alloc]init];
    [self addChildViewController:_homeVC];
    _homeVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
    [self.view addSubview:_homeVC.view];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**< 多少行 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _lDataSource.count;
}
/**< 行高 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

/**< 配置cell */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *const tableViewIdentifier = @"CellIdentifier";
    ButtonTableViewCell *cell = [[ButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
    if (!cell) {
        cell = [[ButtonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
        
    }
    cell.backgroundColor = MAIN_COLOR;
    cell.contentView.backgroundColor = MAIN_COLOR;
    cell.headView.image = [UIImage imageNamed:_lDataSource[indexPath.row]];
    cell.titleLabel.text = _fDataSource[indexPath.row];
    return cell;
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_homeVC removeFromParentViewController];
    [_homeVC.view removeFromSuperview];
    ButtonTableViewCell *cell = (ButtonTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([cell.titleLabel.text isEqualToString:@"首页"]) {
        _homeVC = [[HomeViewController alloc]init];
        _homeVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        [self vcRemove:_homeVC];
        [_vcArray addObject:_homeVC];
    }else if([cell.titleLabel.text isEqualToString:@"金融资讯"]){
        _newsVC = [[NewsViewController alloc]init];
        _newsVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        [self vcRemove:_newsVC];
         [_vcArray addObject:_newsVC];
    }else if([cell.titleLabel.text isEqualToString:@"理财服务"]){
        _serveVC = [[ServeViewController alloc]init];
        _serveVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        [self vcRemove:_serveVC];
        [_vcArray addObject:_serveVC];
    }else if([cell.titleLabel.text isEqualToString:@"银行卡"]){
        _cardVC = [[CardViewController alloc]init];
        _cardVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        [self vcRemove:_cardVC];
        [_vcArray addObject:_cardVC];
    }else if([cell.titleLabel.text isEqualToString:@"缴费服务"]){
        _payVC = [[PayViewController alloc]init];
        _payVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        [self vcRemove:_payVC];
        [_vcArray addObject:_payVC];
    }else if([cell.titleLabel.text isEqualToString:@"代理产品"]){
        _goodsVC = [[GoodsViewController alloc]init];
        _goodsVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        [self vcRemove:_goodsVC];
        [_vcArray addObject:_goodsVC];
    }else if([cell.titleLabel.text isEqualToString:@"我的网银"]){
        _myVC = [[MyViewController alloc]init];
        _myVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        [self vcRemove:_myVC];
        [_vcArray addObject:_myVC];
    }else if([cell.titleLabel.text isEqualToString:@"活动交流"]){
        _talkVC = [[TalkViewController alloc]init];
        _talkVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        [self vcRemove:_talkVC];
        [_vcArray addObject:_talkVC];
    }else if([cell.titleLabel.text isEqualToString:@"日程提醒"]){
        _tipVC = [[TipsViewController alloc]init];
        _tipVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        
        [self vcRemove:_tipVC];
        [_vcArray addObject:_tipVC];
    }else if([cell.titleLabel.text isEqualToString:@"业务咨询"]){
        _questionVC = [[QuestionViewController alloc]init];
        _questionVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        
        [self vcRemove:_questionVC];
        [_vcArray addObject:_questionVC];
            }
}

//点击button
-(void)handleLoginButtonEvent:(UIButton *)sender
{
    [self.view addSubview:_setButton];
    [self.view addSubview:_label];
    [self.view insertSubview:_label atIndex:2];
    [sender setBackgroundImage:[UIImage imageNamed:@"登出-1"] forState:UIControlStateNormal];
    _lDataSource = @[@"首页-1",@"金融资讯-1",@"理财服务-1",@"银行卡-1",@"缴费服务-1",@"代理产品-1",@"我的网银-1",@"活动交流-1",@"日程提醒-1",@"业务咨询-1"];
    _fDataSource = @[@"首页",@"金融资讯",@"理财服务",@"银行卡",@"缴费服务",@"代理产品",@"我的网银",@"活动交流",@"日程提醒",@"业务咨询"];
    [_mainTableView reloadData];
}

- (void)handleMenuButtonEvent:(UIButton *)sender
{
    if (sender.tag == 201) {
        [UIView animateWithDuration:0.5 animations:^{
            _homeVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _newsVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _serveVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _cardVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _payVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _goodsVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _myVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _talkVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _tipVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _questionVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
            _setVC.view.frame = CGRectMake(170, 0, WIDTH-70, HEIGHT - 20);
        }];
        sender.tag ++;
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _homeVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _newsVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _serveVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _cardVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _payVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _goodsVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _myVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _talkVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _tipVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _questionVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
            _setVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
        }];
        sender.tag --;
    }
    
}

- (void)handleSetButtonEvent
{
    _setVC = [[SetViewController alloc]init];
    _setVC.view.frame = CGRectMake(70, 0, WIDTH-70, HEIGHT - 20);
    [self vcRemove:_setVC];
}

- (void)vcRemove:(BaseViewController *)ViewController
{
    [self addChildViewController:ViewController];
    [self.view addSubview:ViewController.view];
    if (_vcArray == nil) {
    }else{
        for (BaseViewController *vc in _vcArray) {
            [vc removeFromParentViewController];
            [vc.view removeFromSuperview];
        }
    }
}
@end









